package online.shenme.day04.studentManage.service;

import online.shenme.day04.studentManage.dao.StudentDao;
import online.shenme.day04.studentManage.dao.StudentDaoImpl;
import online.shenme.day04.studentManage.domain.Student;

import java.util.ArrayList;

public class StudentServiceImpl implements StudentService {
    StudentDao studentDao=new StudentDaoImpl();
    @Override
    public boolean login(Student student) {
        boolean flag=false;
       if (!findAll().isEmpty()){
           for (int i = 0; i < findAll().size(); i++) {
               if (student.getId().equals(findAll().get(i).getId())&&
                       student.getPsWord().equals(findAll().get(i).getPsWord())){
                   flag=true;
               }
           }
       }
       return flag;
    }

    @Override
    public boolean register(Student student) {
        boolean flag = false;
        if (!isExits(student.getId())) {
            if (studentDao.add(student)) {
                flag = true;
            }
        }
        return flag;
    }
    @Override
    public ArrayList<Student> findAll() {
        return studentDao.all();
    }

    public boolean isExits(String id) {
        boolean flag = false;
        if (!findAll().isEmpty()) {
            for (int i = 0; i < findAll().size(); i++) {
                if (id.equals(findAll().get(i).getId())) {
                    flag=true;
                    break;
                }
            }
        }
        return flag;
    }
}
